using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using MeetingRoom.Web.View;
using MeetingRoom.Web.Presenter;
using MeetingRoom.Domain;

public partial class View_Apply : System.Web.UI.UserControl
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!this.IsPostBack)
        {
            for (int year = 2007; year < 2020; year++)
            {
                ListItem item = new ListItem();
                item.Text = string.Format("{0}", year);
                item.Value = string.Format("{0}", year);
                this.DropDownListToYear.Items.Add(item);
                this.DropDownListToYear.Items.Add(item);
            }
            for (int month = 1; month <= 12; month++)
            {
                this.DropDownListFromMonth.Items.Add(
                    new ListItem(Convert.ToString(month), Convert.ToString(month)));
                this.DropDownListToMonth.Items.Add(
                    new ListItem(Convert.ToString(month), Convert.ToString(month)));
            }
            this.CalendarFromDate.Visible = false;
            this.CalendarToDate.Visible = false;
        }
    }

    protected ApplyPresenter presenter = null;

    protected Schedule schedule = null;

    protected MeetingRoom.Domain.MeetingRoomType[] meetingRoomTypes = null;

    #region IApplyView Members

    public void SetPresenter(ApplyPresenter presenter)
    {
        this.presenter = presenter;
    }

    public MeetingRoom.Domain.Schedule Schedule
    {
        get
        {
            return this.schedule;
        }
    }

    public MeetingRoom.Domain.MeetingRoomType[] MeetingRoomTypes
    {
        set
        {
            this.meetingRoomTypes = value;
            foreach (MeetingRoom.Domain.MeetingRoomType meetingRoomType 
                in this.meetingRoomTypes)
            {
                this.DropDownListAvailableRoomSize.Items.Add(
                    new ListItem(
                    meetingRoomType.Size.ToString(), 
                    meetingRoomType.Id.ToString()));
            }
        }
        get
        {
            return this.meetingRoomTypes;
        }
    }

    public string Message
    {
        set
        {
            this.LabelMessage.Text = value;
        }
    }

    protected MeetingRoom.Domain.MeetingRoomType GetMeetingRoomTypeById(int id)
    {
        foreach (MeetingRoom.Domain.MeetingRoomType meetingRoomType in this.meetingRoomTypes)
        {
            if (meetingRoomType.Id == id)
            {
                return meetingRoomType;
            }
        }

        return null;
    }

    #endregion

    protected void Submit_Click(object sender, EventArgs e)
    {
        int selected = Int32.Parse(this.DropDownListAvailableRoomSize.SelectedItem.Value);
        this.schedule = new Schedule();
        this.schedule.Name = this.ScheduleNameTextBox.Text;

        this.schedule.Duration = new Duration(
            new DateTime(
            Int32.Parse(this.DropDownListFromYear.SelectedValue), 
            Int32.Parse(this.DropDownListFromMonth.SelectedValue), 
            this.CalendarFromDate.SelectedDate.Day, 
            Int32.Parse(this.TextBoxFrom.Text),
            0,0),
            new DateTime(
            Int32.Parse(this.DropDownListToYear.SelectedValue),
            Int32.Parse(this.DropDownListToMonth.SelectedValue),
            this.CalendarToDate.SelectedDate.Day,
            Int32.Parse(this.TextBoxTo.Text),
            0,0));

        this.schedule.MeetingRoomType = new MeetingRoomType();
        this.schedule.MeetingRoomType.Size = 
            Int32.Parse(this.DropDownListAvailableRoomSize.SelectedValue);
        this.schedule.MeetingRoomType.Projector = 
            Boolean.Parse(this.DropDownListProjector.SelectedValue);

        this.presenter.Apply();
    }

    protected void ShowCalendar_Click(object sender, EventArgs e)
    {
        this.CalendarFromDate.Visible = true;
    }

    protected void ButtonFromDate_Click(object sender, EventArgs e)
    {
        this.CalendarFromDate.Visible = true;
    }

    protected void ButtonToDate_Click(object sender, EventArgs e)
    {
        this.CalendarToDate.Visible = true;
    }

    protected void CalendarFromDate_SelectionChanged(object sender, EventArgs e)
    {
        this.LabelFromDate.Text = this.CalendarFromDate.SelectedDate.Day.ToString();
        this.CalendarFromDate.Visible = false;
    }

    protected void CalendarToDate_SelectionChanged(object sender, EventArgs e)
    {
        this.LabelToDate.Text = this.CalendarToDate.SelectedDate.Day.ToString();
        this.CalendarToDate.Visible = false;
    }

    protected void DropDownListFromYear_SelectedIndexChanged(object sender, EventArgs e)
    {

    }

    protected void ButtonSyncYear_Click(object sender, EventArgs e)
    {
        this.DropDownListToYear.SelectedIndex = this.DropDownListFromYear.SelectedIndex;
    }

    protected void ButtonSyncMonth_Click(object sender, EventArgs e)
    {
        this.DropDownListToMonth.SelectedIndex = this.DropDownListFromMonth.SelectedIndex;
    }
    protected void DropDownListFromMonth_SelectedIndexChanged(object sender, EventArgs e)
    {

    }
    protected void ButtonToDate_Click1(object sender, EventArgs e)
    {
        this.CalendarToDate.Visible = true;
    }
}
